<%@ page import="bean.Email"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.Map"%>


<div id="header">
<h1>Emails</h1>
</div>

<div id="user-view-emails">
<%
	Email[] emails = (Email[]) request.getAttribute("emails");
	HashMap myMap = new HashMap();	
	
	for (Email email : emails) {
		String subjectWithBracket = email.getSubject();
		String letter = "[";
		int i = subjectWithBracket.indexOf(letter);
		//String subject = subjectWithBracket.substring(0,i);
		String accountIdentifier = subjectWithBracket.substring(i+1, subjectWithBracket.length()-1); 
		
		if(myMap.containsKey(accountIdentifier)){
			ArrayList<Email> t = (ArrayList)myMap.get(accountIdentifier);
			t.add(email);	
				}else{
			ArrayList<Email> temp = new ArrayList<Email>();
			temp.add(email);
			myMap.put(accountIdentifier,temp);
		}	
	}

	//retrieve each email and display values
	Iterator iterator = myMap.keySet().iterator();
	while(iterator.hasNext()){        
		String key = (String) iterator.next();
		ArrayList<Email> emailsInHashMapIndex = (ArrayList)myMap.get(key);
		
		for (Email email : emailsInHashMapIndex) {
	String s = email.getSender().replace("<","&lt;").toString();
	s = s.replace(">","&gt;").toString();
	
	String r = email.getRecipient().replace("<","&lt;").toString();
	r = r.replace(">","&gt;").toString();
	
	String subjectWithBracket = email.getSubject();
	String letter = "[";
	int i = subjectWithBracket.indexOf(letter);
	String subject = subjectWithBracket.substring(0,i);
	String accountIdentifier = subjectWithBracket.substring(i+1, subjectWithBracket.length()-1);
%>
			
			
			Email ID: <%= email.getEmailID() %><br>
			User ID: <%= email.getUserID() %><br>
			Account Identifier: <%=accountIdentifier %><br>
			Timestamp: <%= email.getTimestamp() %><br>
			Sender: <%= s %><br>
			Recipient: <%= r %><br>
			Subject: <%= subject %><br>
			Content: <%= email.getContent() %><br><br>			
			
			
			<%
		}
       
	}

   


	
%>
</div>